package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.soms.entity.BorrowBook;
import com.dingreading.cloud.soms.entity.table.BorrowBookTableDef;
import com.dingreading.cloud.soms.mapper.BorrowBookMapper;
import com.dingreading.cloud.soms.service.BorrowBookService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.dingreading.cloud.soms.entity.table.BorrowBookLogTableDef.borrowBookLog;
import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class BorrowBookServiceImpl extends BaseServiceImpl<BorrowBookMapper, BorrowBook, BorrowBookTableDef> implements BorrowBookService {

    @Override
    protected BorrowBookTableDef getTable() {
        return BorrowBookTableDef.borrowBook;
    }

    @Override
    public Page<BorrowBook> pageList(PageUtil pageUtil, String storeUid, String keyword, Integer returnStatus,
                                     String beginTime, String endTime, Integer searchType) {
        QueryCondition condition = table.storeUid.eq(storeUid);
        if (StringUtils.isNotBlank(keyword)) {
            QueryWrapper wrapper = QueryWrapper.create()
                    .select(QueryMethods.distinct(borrowBookLog.borrowBookId))
                    .from(borrowBookLog)
                    .where(borrowBookLog.name.like(keyword));

            condition.and(members.memberName.like(keyword)
                    .or(table.mob.like(keyword))
                    .or(table.id.in(wrapper)));
        }
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginTime), QueryFiledUtil.appendMin24(endTime)));
        }
        if (searchType == null) {
            if (returnStatus != null) {
                condition.and(table.status.eq(returnStatus));
            }
        } else {
            condition.and(table.status.eq(1));
            // 查询类型 1.2天内快到期借阅 2.已过期借阅
            Date date = new Date();
            if (searchType.equals(1)) {
                String bgnDate = DateUtil.getDefaultTime2DateStr(date);
                String endDate = DateUtil.getDefaultTime2DateStr(DateUtil.addDays(date, 2));
                condition.and(table.bookTime.between(bgnDate, endDate));
            } else {
                String dateStr = DateUtil.getDefaultTime2DateStr(date);
                condition.and(table.bookTime.le(dateStr));
            }
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, members.memberName.as("newMemberName"))
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public BorrowBook getByStoreUidAndId(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid).and(table.id.eq(id));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public int saveRemark(String storeUid, Long id, String returnRemark) {
        BorrowBook data = UpdateEntity.of(BorrowBook.class);

        UpdateWrapper<BorrowBook> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.returnRemark, returnRemark);

        QueryCondition condition = table.storeUid.eq(storeUid).and(table.id.eq(id));

        return getMapper().updateByCondition(data, condition);

    }

    @Override
    public int setReturn(String storeUid, Long id, Integer returnNum, Integer borrowNum, Date time) {
        QueryCondition condition = table.storeUid.eq(storeUid).and(table.id.eq(id));

        BorrowBook data = UpdateEntity.of(BorrowBook.class);

        returnNum += 1;
        UpdateWrapper<BorrowBook> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.returnNum, returnNum);
        if (returnNum.equals(borrowNum)) {
            wrapper.set(table.status, 2);
            wrapper.set(table.returnTime, time);
        }

        return getMapper().updateByCondition(data, condition);
    }

    @Override
    public long getBorrowingCnt(String storeUid, String memberUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.memberUid.eq(memberUid))
                .and(table.status.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.id)
                .from(table)
                .where(condition);

        return getMapper().selectCountByQuery(wrapper);
    }

}
